/*
 * NormMediaWriter.h
 *
 *  Created on: 2018年9月28日
 *      Author: zhengboyuan
 */

#ifndef NORMMEDIAWRITER_H_
#define NORMMEDIAWRITER_H_

#include "MediaWriter.h"
#include "PsParser.h"
#include "Ffmpeg.h"
#include "TCriticalSection.h"


namespace av
{


class NormMediaWriter : public MediaWriter
{
public:
    NormMediaWriter(MediaFileFormat fileFormat, MediaWriter* writer);
    virtual ~NormMediaWriter();

	virtual bool open(const char* filename, const MFormat& fmt);

	virtual void close();

	virtual bool isOpen();

	virtual bool getFormat(MFormat& fmt);

	virtual bool writeVideo(uint8_t* data, int size, int64_t pts, int flags);

	virtual bool writeAudio(uint8_t* data, int size, int64_t pts, int flags);

	virtual int getDuration();

	virtual int64_t getByteLength();

	virtual bool setFile(const char* filepath);

	virtual bool setOutputAudioCodec(int codecId);

	virtual void setListener(MediaWriterListener* listener);

	virtual bool setMeta(const char* key, const char* value, int size);

protected:
	bool writePs(MPacket& pkt);

	bool writePacket(MPacket& pkt);


	bool openParser(int codec);
	void closeParser();
	bool isParserOpen();

	bool parseWrite(MPacket& pkt);

	void fixPacketFlags(MPacket& packet);

protected:
	comn::CriticalSection	m_cs;

	MediaFileFormat m_fileFormat;
	MediaWriter* m_writer;

	PsParser	m_psParser;

	AVCodecParserContext* m_parser;
	AVCodecContext* m_pContext;

	std::string	m_filename;
	
	MediaFormat	m_originFormat;
	MediaFormat m_format;

	int64_t	m_startVideoPts;
	int64_t	m_startAudioPts;

};


} /* namespace av */

#endif /* NORMMEDIAWRITER_H_ */
